package com.esri.android.map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import androidx.core.app.NotificationCompat;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.MultiPath;
import com.esri.core.geometry.MultiPoint;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.RuntimeHelper;
import com.esri.core.symbol.Symbol;
import com.github.mikephil.charting.utils.Utils;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class MapSurface extends GLSurfaceView implements GLSurfaceView.Renderer {
    private static final HashSet<String> r;
    private static final int w = 12440;
    final List<Layer> a;
    final AtomicBoolean b;
    WeakReference<com.esri.android.map.c> c;
    final transient Handler d;
    final AtomicInteger e;
    SpatialReference f;
    final c g;
    final AtomicInteger h;
    final transient com.esri.core.internal.tasks.c i;
    SizeChangedListener j;
    boolean k;
    volatile long l;
    volatile long m;
    final String n;
    volatile b o;
    Runnable p;
    private float q;
    private volatile boolean s;
    private Envelope t;
    private boolean u;
    private MapAnimationListener v;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public interface SizeChangedListener {
        void onMapSurfaceSizeChanged();
    }

    private native long nativeDeviceCreate();

    private native long nativeMapCreate(String str, int i, int i2, float f, boolean z);

    private native double nativeMapGetResolution(long j);

    private native double nativeMapGetScale(long j);

    private native boolean nativeMapPan(long j, float f, float f2, boolean z);

    private native void nativeMapPause(long j, boolean z, long j2);

    /* JADX INFO: Access modifiers changed from: private */
    public native void nativeMapRelease(long j);

    private native void nativeMapRender(long j, long j2);

    private native boolean nativeMapRenderToFile(long j, float f, float f2, Bitmap bitmap, long j2);

    private native boolean nativeMapSetResolution(long j, double d2, boolean z);

    private native boolean nativeMapSetResolutionAt(long j, double d2, float f, float f2, boolean z);

    private native boolean nativeMapSetScale(long j, double d2, boolean z);

    private native boolean nativeMapSetScaleAt(long j, double d2, double d3, double d4);

    private native void nativeMapSetup(long j, int i, int i2);

    private native void nativeMapUnPause(long j, long j2);

    private native boolean nativeMapZoom(long j, double d2);

    private native boolean nativeMapZoomAt(long j, double d2, float f, float f2, boolean z);

    native void nativeCreateGraphicImage(long j, String str, int i, Bitmap bitmap, byte[] bArr);

    native void nativeCreateGraphicImages(long j, int i, String[] strArr, Object[] objArr, int i2, int i3, int i4, Bitmap[] bitmapArr);

    native void nativeDisableWrapAround(long j);

    native void nativeDisplayMagnifierAt(long j, float f, float f2);

    native void nativeEnableWrapAround(long j);

    native void nativeForceRedraw(long j);

    native long nativeGetGPSLayer(long j);

    native double nativeGetMaxResolution(long j);

    native double nativeGetMaxScale(long j);

    native double nativeGetMinResolution(long j);

    native double nativeGetMinScale(long j);

    native void nativeInitMagnifier(long j, Bitmap bitmap, Bitmap bitmap2);

    native boolean nativeIsWrapAroundEnabled(long j);

    native long nativeLock(long j);

    native int nativeMapAddLayer(long j, long j2, int i);

    native int nativeMapGetLayerCount(long j);

    native double[] nativeMapGetMapAnchor(long j);

    native double[] nativeMapGetMaxExtent(long j);

    native double nativeMapGetOrientation(long j);

    native String[] nativeMapGetSpatialReference(long j);

    native void nativeMapRemoveLayerAtIndex(long j, long j2, int i);

    native void nativeMapSetExtent(long j, double d2, double d3, double d4, double d5);

    native boolean nativeMapSetMapAnchor(long j, double d2, double d3, boolean z);

    native void nativeMapSetMaxExtent(long j, double d2, double d3, double d4, double d5);

    native boolean nativeMapSetOrientation(long j, double d2, boolean z);

    native boolean nativeMapSetOrientationAt(long j, double d2, float f, float f2, boolean z);

    native double[] nativeMapToMapPoint(long j, float f, float f2);

    native float[] nativeMapToScreenPoint(long j, double d2, double d3);

    native double[] nativeQueryExtent(long j);

    native void nativeResourceError(long j, String str);

    native void nativeSetAnimating(long j, boolean z);

    native void nativeSetBackground(long j, int[] iArr, int[] iArr2, float f, float f2);

    native void nativeSetInteracting(long j, boolean z);

    native void nativeSetMaxResolution(long j, double d2);

    native void nativeSetMaxScale(long j, double d2);

    native void nativeSetMinResolution(long j, double d2);

    native void nativeSetMinScale(long j, double d2);

    native void nativeSetResource(long j, String str, byte[] bArr);

    native void nativeSetSelectionColorWidth(long j, int i);

    native boolean nativeSetSpatialReference(long j, int i, String str, double d2, double d3, double d4, double d5);

    native void nativeToggleMagnifier(long j, boolean z);

    native long nativeUnlock(long j);

    @Override // android.opengl.GLSurfaceView.Renderer
    public void onSurfaceCreated(GL10 gl10, EGLConfig eGLConfig) {
    }

    static {
        RuntimeHelper.initialize();
        HashSet<String> hashSet = new HashSet<>();
        r = hashSet;
        hashSet.add("PC36100");
        hashSet.add("Nexus One");
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static class c {
        private static final BitmapFactory.Options i = new BitmapFactory.Options();
        Bitmap a;
        AtomicBoolean b = new AtomicBoolean(false);
        CountDownLatch c = null;
        short d = -1;
        float e = 0.0f;
        float f = 0.0f;
        int g = 0;
        int h = 0;

        c() {
            i.inPurgeable = true;
        }

        public boolean a() {
            return this.d == 1;
        }

        void a(float f, float f2, int i2, int i3) {
            this.e = f;
            this.f = f2;
            this.g = i2;
            this.h = i3;
            this.d = (short) 0;
            this.c = new CountDownLatch(1);
        }

        public Bitmap b() {
            return this.a;
        }
    }

    /* loaded from: classes.dex */
    static final class d {
        Layer a;
        int b;

        public d(Layer layer, int i) {
            this.a = layer;
            this.b = i;
        }
    }

    public MapSurface(Context context, Handler handler) {
        super(context);
        this.a = Collections.synchronizedList(new ArrayList());
        this.b = new AtomicBoolean(false);
        this.e = new AtomicInteger(0);
        this.q = 120.0f;
        this.f = null;
        this.g = new c();
        this.h = new AtomicInteger(0);
        this.i = new com.esri.core.internal.tasks.c();
        this.s = false;
        this.j = null;
        this.k = false;
        this.t = null;
        this.u = false;
        this.l = 0L;
        this.m = 0L;
        this.p = new Runnable() { // from class: com.esri.android.map.MapSurface.1
            @Override // java.lang.Runnable
            public void run() {
                List<Layer> list;
                MapSurface.this.e.set(0);
                if (MapSurface.this.b.compareAndSet(false, true)) {
                    synchronized (this) {
                        long j = MapSurface.this.l;
                        MapSurface.this.l = 0L;
                        if (MapSurface.this.c != null) {
                            try {
                                com.esri.android.map.c cVar = MapSurface.this.c.get();
                                if (cVar != null) {
                                    cVar.cancel(true);
                                }
                                MapSurface.this.c = null;
                            } catch (NullPointerException unused) {
                            }
                        }
                        MapSurface.this.i.a();
                        try {
                            MapSurface.this.a(true, j);
                        } catch (Exception e2) {
                            Log.w(com.esri.core.internal.a.a, "Can not release the context.", e2);
                        }
                        synchronized (MapSurface.this.a) {
                            try {
                                for (Layer layer : MapSurface.this.a) {
                                    layer.recycle();
                                }
                                list = MapSurface.this.a;
                            } catch (Exception e3) {
                                Log.w(com.esri.core.internal.a.a, "Can not release layers.", e3);
                                list = MapSurface.this.a;
                            }
                            list.clear();
                        }
                        if (j != 0) {
                            MapSurface.this.nativeMapRelease(j);
                        }
                    }
                }
            }
        };
        this.d = handler;
        if (isInEditMode()) {
            this.n = null;
            return;
        }
        File a2 = RuntimeHelper.a(context);
        this.n = a2 != null ? a2.getAbsolutePath() : null;
        a(context);
    }

    private void a(Context context) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(displayMetrics);
        this.q = (float) Math.sqrt(displayMetrics.xdpi * displayMetrics.ydpi);
        this.o = new b();
        setEGLContextFactory(this.o);
        setEGLConfigChooser(new a(5, 6, 5, 0, 0, 0));
        setRenderer(this);
        setBackgroundDrawable(new ColorDrawable(0));
        setRenderMode(0);
        this.l = nativeMapCreate(this.n, 100, 100, this.q, Build.VERSION.SDK_INT > 8 && !r.contains(Build.MODEL));
        this.m = nativeDeviceCreate();
    }

    private void requestRender(boolean z) {
        super.requestRender();
        this.s = z;
        if (this.t != null && !z) {
            MapAnimationListener mapAnimationListener = this.v;
            if (mapAnimationListener != null) {
                mapAnimationListener.onAnimationFinished();
            }
            Envelope v = v();
            if (!this.t.equals(v)) {
                this.t = v;
                a();
            }
        }
        MapAnimationListener mapAnimationListener2 = this.v;
        if (mapAnimationListener2 != null) {
            mapAnimationListener2.onAnimating(z);
        }
    }

    private void requestResourceDownload(String str) {
        this.i.b.submit(new e(this, str));
    }

    void a(String str) {
        if (this.l == 0) {
            return;
        }
        nativeResourceError(this.l, str);
    }

    void a(String str, byte[] bArr) {
        if (this.l == 0) {
            return;
        }
        nativeSetResource(this.l, str, bArr);
    }

    public void a() {
        synchronized (this.a) {
            for (Layer layer : this.a) {
                if (layer instanceof ArcGISFeatureLayer) {
                    ((ArcGISFeatureLayer) layer).refresh();
                }
            }
        }
    }

    @Override // android.opengl.GLSurfaceView
    public void finalize() {
        try {
            super.finalize();
        } catch (Throwable unused) {
        }
    }

    private void spatialReferenceInitialized() {
        if (this.e.getAndSet(1) != 1) {
            a(OnStatusChangedListener.STATUS.INITIALIZED);
        }
        j();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean b() {
        return this.e.compareAndSet(1, 1);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean c() {
        return this.s;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean a(float f, float f2, boolean z) {
        this.t = v();
        if (this.l == 0 || !b()) {
            return false;
        }
        this.s = nativeMapPan(this.l, f, f2, z);
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean a(double d2, boolean z) {
        this.t = v();
        if (this.l == 0 || !b()) {
            return false;
        }
        this.s = nativeMapSetScale(this.l, d2, z);
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean a(double d2, Point point) {
        this.t = v();
        if (this.l == 0 || !b() || point == null) {
            return false;
        }
        this.s = nativeMapSetScaleAt(this.l, d2, point.getX(), point.getY());
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public double d() {
        if (this.l == 0 || !b()) {
            return Double.NaN;
        }
        return nativeMapGetScale(this.l);
    }

    public boolean a(double d2) {
        this.t = v();
        if (this.l == 0 || !b()) {
            return false;
        }
        this.s = nativeMapZoom(this.l, d2);
        return true;
    }

    public boolean a(double d2, float f, float f2, boolean z) {
        this.t = v();
        if (this.l == 0 || !b()) {
            return false;
        }
        this.s = nativeMapZoomAt(this.l, d2, f, f2, z);
        return true;
    }

    public void a(boolean z) {
        a(z, this.l);
    }

    synchronized void a(boolean z, long j) {
        if (j != 0) {
            if (z) {
                if (this.h.getAndSet(-1) != -1) {
                    nativeMapPause(j, true, this.m);
                }
            } else if (this.h.compareAndSet(1, 0)) {
                nativeMapPause(j, false, this.m);
            }
        }
    }

    public synchronized void e() {
        if (this.l == 0) {
            this.l = nativeMapCreate(this.n, getWidth(), getHeight(), this.q, Build.VERSION.SDK_INT > 8 && !r.contains(Build.MODEL));
        }
        if (this.l != 0 && this.h.compareAndSet(0, 1)) {
            nativeMapUnPause(this.l, this.m);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class b implements GLSurfaceView.EGLContextFactory {
        b() {
        }

        @Override // android.opengl.GLSurfaceView.EGLContextFactory
        public EGLContext createContext(EGL10 egl10, EGLDisplay eGLDisplay, EGLConfig eGLConfig) {
            MapSurface.a("Before eglCreateContext", egl10);
            EGLContext eglCreateContext = egl10.eglCreateContext(eGLDisplay, eGLConfig, EGL10.EGL_NO_CONTEXT, new int[]{MapSurface.w, 2, 12344});
            MapSurface.a("After eglCreateContext", egl10);
            MapSurface.this.e();
            return eglCreateContext;
        }

        @Override // android.opengl.GLSurfaceView.EGLContextFactory
        public void destroyContext(EGL10 egl10, EGLDisplay eGLDisplay, EGLContext eGLContext) {
            new Thread(MapSurface.this.p).start();
            egl10.eglDestroyContext(eGLDisplay, eGLContext);
        }
    }

    static void a(String str, EGL10 egl10) {
        while (true) {
            int eglGetError = egl10.eglGetError();
            if (eglGetError == 12288) {
                return;
            }
            Log.e(com.esri.core.internal.a.a, String.format("%s: EGL error: 0x%x", str, Integer.valueOf(eglGetError)));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class a implements GLSurfaceView.EGLConfigChooser {
        private static int h = 4;
        private static int[] i = {12324, 4, 12323, 4, 12322, 4, 12352, 4, 12344};
        int a;
        int b;
        int c;
        int d;
        int e;
        int f;
        private int[] g = new int[1];

        public a(int i2, int i3, int i4, int i5, int i6, int i7) {
            this.a = i2;
            this.b = i3;
            this.c = i4;
            this.d = i5;
            this.e = i6;
            this.f = i7;
        }

        @Override // android.opengl.GLSurfaceView.EGLConfigChooser
        public EGLConfig chooseConfig(EGL10 egl10, EGLDisplay eGLDisplay) {
            int[] iArr = new int[1];
            egl10.eglChooseConfig(eGLDisplay, i, null, 0, iArr);
            int i2 = iArr[0];
            if (i2 <= 0) {
                throw new IllegalArgumentException("No configs match configSpec");
            }
            EGLConfig[] eGLConfigArr = new EGLConfig[i2];
            egl10.eglChooseConfig(eGLDisplay, i, eGLConfigArr, i2, iArr);
            return a(egl10, eGLDisplay, eGLConfigArr);
        }

        public EGLConfig a(EGL10 egl10, EGLDisplay eGLDisplay, EGLConfig[] eGLConfigArr) {
            for (EGLConfig eGLConfig : eGLConfigArr) {
                int a = a(egl10, eGLDisplay, eGLConfig, 12325, 0);
                int a2 = a(egl10, eGLDisplay, eGLConfig, 12326, 0);
                if (a >= this.e && a2 >= this.f) {
                    int a3 = a(egl10, eGLDisplay, eGLConfig, 12324, 0);
                    int a4 = a(egl10, eGLDisplay, eGLConfig, 12323, 0);
                    int a5 = a(egl10, eGLDisplay, eGLConfig, 12322, 0);
                    int a6 = a(egl10, eGLDisplay, eGLConfig, 12321, 0);
                    if (a3 == this.a && a4 == this.b && a5 == this.c && a6 == this.d) {
                        return eGLConfig;
                    }
                }
            }
            return null;
        }

        private int a(EGL10 egl10, EGLDisplay eGLDisplay, EGLConfig eGLConfig, int i2, int i3) {
            return egl10.eglGetConfigAttrib(eGLDisplay, eGLConfig, i2, this.g) ? this.g[0] : i3;
        }
    }

    @Override // android.opengl.GLSurfaceView.Renderer
    public void onSurfaceChanged(GL10 gl10, int i, int i2) {
        if (this.l != 0) {
            nativeMapSetup(this.l, i, i2);
            SizeChangedListener sizeChangedListener = this.j;
            if (sizeChangedListener != null) {
                sizeChangedListener.onMapSurfaceSizeChanged();
            }
        }
    }

    @Override // android.opengl.GLSurfaceView.Renderer
    public void onDrawFrame(GL10 gl10) {
        long r2;
        c cVar;
        if (this.l != 0) {
            short s = this.g.d;
            if (s != -1) {
                if (s == 0) {
                    if (this.g.g <= 0 || this.g.h <= 0) {
                        return;
                    }
                    synchronized (this.g) {
                        r2 = r();
                        if (this.g.a != null && this.g.g == this.g.a.getWidth() && this.g.h == this.g.a.getHeight()) {
                            nativeMapRenderToFile(this.l, this.g.e, this.g.f, this.g.a, this.m);
                        } else {
                            Bitmap createBitmap = Bitmap.createBitmap(this.g.g, this.g.h, Bitmap.Config.ARGB_8888);
                            nativeMapRenderToFile(this.l, this.g.e, this.g.f, createBitmap, this.m);
                            try {
                                if (this.g.a != null && !this.g.a.isRecycled()) {
                                    this.g.a.recycle();
                                }
                                cVar = this.g;
                            } catch (Exception unused) {
                                cVar = this.g;
                            }
                            cVar.a = null;
                            this.g.a = createBitmap;
                        }
                    }
                    this.g.c.countDown();
                    return;
                } else if (s != 1) {
                    return;
                }
            }
            r2 = r();
            try {
                nativeMapRender(r2, this.m);
            } finally {
                a(r2);
            }
        }
    }

    void a(OnStatusChangedListener.STATUS status) {
        Message obtainMessage = this.d.obtainMessage();
        Bundle bundle = new Bundle();
        bundle.putInt(NotificationCompat.CATEGORY_STATUS, status.getValue());
        obtainMessage.setData(bundle);
        obtainMessage.obj = this;
        this.d.sendMessage(obtainMessage);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public int a(Layer layer, int i) {
        if (this.l != 0 && layer.nativeHandle != 0) {
            if (layer instanceof TiledLayer) {
                a(layer, TiledLayer.a);
            } else if (layer instanceof DynamicLayer) {
                a(layer, DynamicLayer.a);
            }
            int nativeMapAddLayer = nativeMapAddLayer(this.l, layer.nativeHandle, i);
            if (nativeMapAddLayer >= 0) {
                layer.n = this.d;
                layer.m = this;
                if (layer instanceof GroupLayer) {
                    a((GroupLayer) layer);
                }
                if (i < 0) {
                    this.a.add(layer);
                    return nativeMapAddLayer;
                }
                this.a.add(i, layer);
                return nativeMapAddLayer;
            }
            throw new RuntimeException("Failed to add the layer: " + layer);
        }
        Log.e(com.esri.core.internal.a.a, "The map or layer has been destroyed or recycled.");
        return -1;
    }

    private void a(GroupLayer groupLayer) {
        Layer[] layers = groupLayer.getLayers();
        if (layers == null || layers.length <= 0) {
            return;
        }
        for (Layer layer : layers) {
            if (layer instanceof GroupLayer) {
                a((GroupLayer) layer);
            } else {
                layer.n = this.d;
                layer.m = this;
                if (layer instanceof TiledLayer) {
                    a(layer, TiledLayer.a);
                } else if (layer instanceof DynamicLayer) {
                    a(layer, DynamicLayer.a);
                }
            }
        }
    }

    private void a(Layer layer, float f) {
        if (layer.isRenderNativeResolution()) {
            layer.setRenderPixelsPerInch(this.q);
            return;
        }
        float f2 = this.q;
        if (f2 >= f) {
            layer.setRenderPixelsPerInch(f);
        } else {
            layer.setRenderPixelsPerInch(f2);
        }
    }

    public void a(int i) {
        Layer layer;
        if (this.l == 0 || !b() || (layer = this.a.get(i)) == null) {
            return;
        }
        nativeMapRemoveLayerAtIndex(this.l, layer.nativeHandle, i);
        layer.n = null;
        layer.m = null;
        this.a.remove(i);
    }

    public double f() {
        if (this.l == 0 || !b()) {
            return Double.NaN;
        }
        return nativeMapGetResolution(this.l);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Point g() {
        double[] nativeMapGetMapAnchor;
        if (this.l == 0 || !b() || (nativeMapGetMapAnchor = nativeMapGetMapAnchor(this.l)) == null || nativeMapGetMapAnchor.length != 2) {
            return null;
        }
        return new Point(nativeMapGetMapAnchor[0], nativeMapGetMapAnchor[1]);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean a(Point point, boolean z) {
        this.t = v();
        if (this.l == 0 || point == null || point.isEmpty()) {
            return false;
        }
        this.s = nativeMapSetMapAnchor(this.l, point.getX(), point.getY(), z);
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Envelope h() {
        double[] nativeMapGetMaxExtent;
        if (this.l == 0 || (nativeMapGetMaxExtent = nativeMapGetMaxExtent(this.l)) == null || nativeMapGetMaxExtent.length != 4) {
            return null;
        }
        return new Envelope(nativeMapGetMaxExtent[0], nativeMapGetMaxExtent[1], nativeMapGetMaxExtent[2], nativeMapGetMaxExtent[3]);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a(Envelope envelope) {
        if (this.l == 0 || envelope == null || envelope.isEmpty()) {
            return;
        }
        nativeMapSetMaxExtent(this.l, envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax());
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a(int i, int i2, float f, float f2) {
        if (this.l != 0) {
            nativeSetBackground(this.l, new int[]{Color.red(i), Color.green(i), Color.blue(i), Color.alpha(i)}, new int[]{Color.red(i2), Color.green(i2), Color.blue(i2), Color.alpha(i2)}, f, f2);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean a(SpatialReference spatialReference, Envelope envelope) {
        if (this.l != 0) {
            return nativeSetSpatialReference(this.l, spatialReference.getID(), spatialReference.getText(), envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax());
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean a(Point point, double d2, boolean z) {
        this.t = v();
        if (this.l == 0 || !b()) {
            return false;
        }
        nativeMapSetMapAnchor(this.l, point.getX(), point.getY(), z);
        this.s = nativeMapSetResolution(this.l, d2, z);
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean b(double d2, boolean z) {
        this.t = v();
        if (this.l != 0) {
            this.s = nativeMapSetResolution(this.l, d2, z);
            return true;
        }
        return false;
    }

    boolean b(double d2, float f, float f2, boolean z) {
        this.t = v();
        if (this.l != 0) {
            this.s = nativeMapSetResolutionAt(this.l, d2, f, f2, z);
            return true;
        }
        return false;
    }

    public boolean a(Geometry geometry, int i, boolean z) {
        Point[] pointArr;
        Point a2;
        int i2 = 0;
        if (geometry != null && !geometry.isEmpty()) {
            this.t = v();
            if (this.l != 0) {
                if (!b()) {
                    Envelope envelope = new Envelope();
                    geometry.queryEnvelope(envelope);
                    if (!envelope.isEmpty() && envelope.getWidth() > Utils.DOUBLE_EPSILON && envelope.getHeight() > Utils.DOUBLE_EPSILON) {
                        nativeMapSetExtent(this.l, envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax());
                    }
                } else {
                    Point[] pointArr2 = null;
                    if (geometry instanceof Envelope) {
                        Envelope envelope2 = (Envelope) geometry;
                        pointArr2 = new Point[]{envelope2.getUpperLeft(), envelope2.getUpperRight(), envelope2.getLowerLeft(), envelope2.getLowerRight()};
                    } else {
                        if (geometry instanceof MultiPath) {
                            MultiPath multiPath = (MultiPath) geometry;
                            int pointCount = multiPath.getPointCount();
                            pointArr = new Point[pointCount];
                            for (int i3 = 0; i3 < pointCount; i3++) {
                                pointArr[i3] = multiPath.getPoint(i3);
                            }
                        } else if (geometry instanceof MultiPoint) {
                            MultiPoint multiPoint = (MultiPoint) geometry;
                            int pointCount2 = multiPoint.getPointCount();
                            pointArr = new Point[pointCount2];
                            for (int i4 = 0; i4 < pointCount2; i4++) {
                                pointArr[i4] = multiPoint.getPoint(i4);
                            }
                        } else if (geometry instanceof Point) {
                            return a((Point) geometry, z);
                        }
                        pointArr2 = pointArr;
                    }
                    if (pointArr2 != null && pointArr2.length > 1) {
                        double f = f();
                        if (!Double.isNaN(f) && f > Utils.DOUBLE_EPSILON && (a2 = a((Point) pointArr2[0].copy())) != null && !a2.isEmpty()) {
                            Point point = (Point) a2.copy();
                            for (Point point2 : pointArr2) {
                                Point a3 = a(point2);
                                if (a3 == null || a3.isEmpty()) {
                                    return false;
                                }
                                if (a3.getX() < a2.getX()) {
                                    a2.setX(a3.getX());
                                } else if (a3.getX() > point.getX()) {
                                    point.setX(a3.getX());
                                }
                                if (a3.getY() < a2.getY()) {
                                    a2.setY(a3.getY());
                                } else if (a3.getY() > point.getY()) {
                                    point.setY(a3.getY());
                                }
                            }
                            Point b2 = b(new Point(((point.getX() - a2.getX()) / 2.0d) + a2.getX(), a2.getY() + ((point.getY() - a2.getY()) / 2.0d)));
                            if (b2 != null && !b2.isEmpty()) {
                                int width = getWidth();
                                int height = getHeight();
                                if (i > 0 && (width > i || height > i)) {
                                    i2 = i;
                                }
                                double x = point.getX() - a2.getX();
                                double d2 = width - i2;
                                Double.isNaN(d2);
                                double d3 = x / d2;
                                double y = point.getY() - a2.getY();
                                double d4 = height - i2;
                                Double.isNaN(d4);
                                double d5 = y / d4;
                                if (d3 <= d5) {
                                    d3 = d5;
                                }
                                return a(b2, d3 * f, z);
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    public Polygon i() {
        double[] nativeQueryExtent;
        if (this.l == 0 || (nativeQueryExtent = nativeQueryExtent(this.l)) == null || nativeQueryExtent.length != 4 || Double.isNaN(nativeQueryExtent[0]) || Double.isNaN(nativeQueryExtent[1]) || Double.isNaN(nativeQueryExtent[2]) || Double.isNaN(nativeQueryExtent[3])) {
            return null;
        }
        Polygon polygon = new Polygon();
        polygon.startPath(new Point(nativeQueryExtent[0], nativeQueryExtent[1]));
        polygon.lineTo(new Point(nativeQueryExtent[2], nativeQueryExtent[1]));
        polygon.lineTo(new Point(nativeQueryExtent[2], nativeQueryExtent[3]));
        polygon.lineTo(new Point(nativeQueryExtent[0], nativeQueryExtent[3]));
        polygon.closeAllPaths();
        return polygon;
    }

    private Envelope v() {
        double[] nativeQueryExtent;
        if (this.l == 0 || (nativeQueryExtent = nativeQueryExtent(this.l)) == null || nativeQueryExtent.length != 4 || Double.isNaN(nativeQueryExtent[0]) || Double.isNaN(nativeQueryExtent[1]) || Double.isNaN(nativeQueryExtent[2]) || Double.isNaN(nativeQueryExtent[3])) {
            return null;
        }
        return new Envelope(nativeQueryExtent[0], nativeQueryExtent[1], nativeQueryExtent[2], nativeQueryExtent[3]);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Point a(Point point) {
        float[] nativeMapToScreenPoint;
        if (this.l == 0 || !b() || point == null || (nativeMapToScreenPoint = nativeMapToScreenPoint(this.l, point.getX(), point.getY())) == null || nativeMapToScreenPoint.length != 2) {
            return null;
        }
        return new Point(nativeMapToScreenPoint[0], nativeMapToScreenPoint[1]);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Point b(Point point) {
        double[] nativeMapToMapPoint;
        if (this.l == 0 || !b() || point == null || (nativeMapToMapPoint = nativeMapToMapPoint(this.l, (float) point.getX(), (float) point.getY())) == null || nativeMapToMapPoint.length != 2) {
            return null;
        }
        return new Point(nativeMapToMapPoint[0], nativeMapToMapPoint[1]);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Point a(float f, float f2) {
        double[] nativeMapToMapPoint;
        if (this.l == 0 || !b() || (nativeMapToMapPoint = nativeMapToMapPoint(this.l, f, f2)) == null || nativeMapToMapPoint.length != 2) {
            return null;
        }
        return new Point(nativeMapToMapPoint[0], nativeMapToMapPoint[1]);
    }

    public SpatialReference j() {
        String[] nativeMapGetSpatialReference;
        if (this.f == null && this.l != 0 && b() && (nativeMapGetSpatialReference = nativeMapGetSpatialReference(this.l)) != null && nativeMapGetSpatialReference.length == 2) {
            if (nativeMapGetSpatialReference[0] != null) {
                try {
                    int parseInt = Integer.parseInt(nativeMapGetSpatialReference[0]);
                    if (parseInt > 0) {
                        SpatialReference create = SpatialReference.create(parseInt);
                        this.f = create;
                        return create;
                    }
                } catch (NumberFormatException unused) {
                }
            }
            if (nativeMapGetSpatialReference[1] != null && nativeMapGetSpatialReference[1].length() > 0) {
                this.f = SpatialReference.create(nativeMapGetSpatialReference[1]);
            }
        }
        return this.f;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void k() {
        if (this.l == 0 || !b()) {
            return;
        }
        int size = this.a.size();
        for (Layer layer : this.a) {
            if (layer.nativeHandle != 0) {
                size--;
                nativeMapRemoveLayerAtIndex(this.l, layer.nativeHandle, size);
            }
        }
        this.a.clear();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Layer[] l() {
        List<Layer> list = this.a;
        return (Layer[]) list.toArray(new Layer[list.size()]);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean c(double d2, float f, float f2, boolean z) {
        this.t = v();
        if (this.l == 0 || !b()) {
            return false;
        }
        this.s = nativeMapSetOrientationAt(this.l, d2, f, f2, z);
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean c(double d2, boolean z) {
        this.t = v();
        if (this.l == 0 || !b()) {
            return false;
        }
        this.s = nativeMapSetOrientation(this.l, d2, z);
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public double m() {
        return (this.l == 0 || !b()) ? Utils.DOUBLE_EPSILON : nativeMapGetOrientation(this.l);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public double n() {
        if (this.l != 0) {
            return nativeGetMinResolution(this.l);
        }
        return Double.NaN;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void b(double d2) {
        if (this.l != 0) {
            nativeSetMinResolution(this.l, d2);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public double o() {
        if (this.l != 0) {
            return nativeGetMaxResolution(this.l);
        }
        return Double.NaN;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void c(double d2) {
        if (this.l != 0) {
            nativeSetMaxResolution(this.l, d2);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public float p() {
        return this.q;
    }

    private void w() {
        nativeForceRedraw(this.l);
        this.g.b.set(false);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Bitmap a(float f, float f2, int i, int i2) {
        if (this.l != 0) {
            this.g.a(f, f2, i, i2);
            if (this.g.b.compareAndSet(false, true)) {
                w();
            }
            try {
                this.g.c.await(30L, TimeUnit.SECONDS);
                return this.g.b();
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            } finally {
                this.g.d = (short) -1;
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Bitmap a(Symbol symbol, Geometry geometry, int i, int i2, int i3) {
        if (this.l == 0 || symbol == null || geometry == null) {
            return null;
        }
        Bitmap createBitmap = Bitmap.createBitmap(i, i2, Bitmap.Config.ARGB_8888);
        try {
            nativeCreateGraphicImage(this.l, symbol.toJson(), Color.argb(Color.alpha(i3), Color.blue(i3), Color.green(i3), Color.red(i3)), createBitmap, GeometryEngine.geometryToEsriShape(geometry));
            return createBitmap;
        } catch (Exception e2) {
            Log.e(com.esri.core.internal.a.a, "Can not create the swatch for the symbol.", e2);
            throw new IllegalArgumentException(e2);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Bitmap[] a(Symbol[] symbolArr, Geometry[] geometryArr, int i, int i2, int i3) {
        Bitmap[] bitmapArr = new Bitmap[0];
        if (this.l == 0 || symbolArr == null || geometryArr == null || symbolArr.length != geometryArr.length) {
            return bitmapArr;
        }
        Bitmap[] bitmapArr2 = new Bitmap[symbolArr.length];
        int length = symbolArr.length;
        String[] strArr = new String[length];
        Object[] objArr = new Object[symbolArr.length];
        for (int i4 = 0; i4 < symbolArr.length; i4++) {
            try {
                strArr[i4] = symbolArr[i4].toJson();
                objArr[i4] = GeometryEngine.geometryToEsriShape(geometryArr[i4]);
                bitmapArr2[i4] = Bitmap.createBitmap(i, i2, Bitmap.Config.ARGB_8888);
            } catch (Exception e2) {
                Log.e(com.esri.core.internal.a.a, "Can not create the swatch for the symbol.", e2);
                throw new IllegalArgumentException(e2);
            }
        }
        nativeCreateGraphicImages(this.l, length, strArr, objArr, i, i2, Color.argb(Color.alpha(i3), Color.blue(i3), Color.green(i3), Color.red(i3)), bitmapArr2);
        return bitmapArr2;
    }

    long q() {
        if (this.l != 0) {
            return nativeGetGPSLayer(this.l);
        }
        return 0L;
    }

    protected synchronized long r() {
        if (this.l != 0) {
            return nativeLock(this.l);
        }
        return 0L;
    }

    void a(long j) {
        if (j != 0) {
            nativeUnlock(j);
        }
    }

    public void b(boolean z) {
        this.k = z;
        if (this.l != 0) {
            if (z) {
                nativeEnableWrapAround(this.l);
            } else {
                nativeDisableWrapAround(this.l);
            }
        }
    }

    public boolean s() {
        if (this.k && this.l != 0) {
            return nativeIsWrapAroundEnabled(this.l);
        }
        return this.k;
    }

    public void a(Bitmap bitmap, Bitmap bitmap2) {
        if (this.l != 0) {
            nativeInitMagnifier(this.l, bitmap, bitmap2);
        }
    }

    public void b(float f, float f2) {
        if (this.l != 0) {
            nativeDisplayMagnifierAt(this.l, f, f2);
        }
    }

    public void c(boolean z) {
        if (this.l != 0) {
            nativeToggleMagnifier(this.l, z);
        }
    }

    public void d(boolean z) {
        if (this.l == 0 || z == this.u) {
            return;
        }
        this.u = z;
        nativeSetInteracting(this.l, z);
    }

    public void b(int i) {
        if (this.l != 0) {
            nativeSetSelectionColorWidth(this.l, i);
        }
    }

    public void d(double d2) {
        if (this.l != 0) {
            nativeSetMaxScale(this.l, d2);
        }
    }

    public double t() {
        return this.l != 0 ? nativeGetMaxScale(this.l) : Utils.DOUBLE_EPSILON;
    }

    public void e(double d2) {
        if (this.l != 0) {
            nativeSetMinScale(this.l, d2);
        }
    }

    public double u() {
        return this.l != 0 ? nativeGetMinScale(this.l) : Utils.DOUBLE_EPSILON;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a(MapAnimationListener mapAnimationListener) {
        this.v = mapAnimationListener;
    }

    /* loaded from: classes.dex */
    private class e implements Runnable {
        private String b;
        private MapSurface c;

        public e(MapSurface mapSurface, String str) {
            this.c = mapSurface;
            this.b = str;
        }

        @Override // java.lang.Runnable
        public void run() {
            try {
                this.c.a(this.b, com.esri.core.internal.io.handler.a.a(this.b, (Map<String, String>) null));
            } catch (Exception unused) {
                this.c.a(this.b);
            }
        }
    }
}
